﻿Imports System.Web.Configuration
Imports System.Web.Services

Public Class BidangDokumen
    Inherits System.Web.UI.Page

    Dim connectionString As String
    Dim bidangDokumenDb As BidangDokumenDb
    Shared CurrentID As String
    Shared CurrentBidangDokumen As String

    Dim koneksidataakses2 As New KoneksiDataAkses
    'Dim tracking As New track_history
    Dim kueri As String
    Dim kueri2 As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connectionString = WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString
        bidangDokumenDb = New BidangDokumenDb(connectionString)

        BidangDokumenGridView.DataBind()
        BidangDokumenTabelForm.Visible = False
        AddButton.Enabled = True
    End Sub

    Sub ResetState()
        AddButton.Enabled = True
        BidangDokumenTabelForm.Visible = False
        BidangDokumen.Text = ""
        Deskripsi.Text = ""
        BidangDokumenGridView.SelectedIndex = -1
        ID.Text = "0"
    End Sub

    Protected Sub BidangDokumenGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles BidangDokumenGridView.RowCommand
        If e.CommandName = "CopyCommand" Then
            ResetState()
            ID.Text = "0"
            BidangDokumen.Text = BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            Deskripsi.Text = BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            If BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(3).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(3).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If
            BidangDokumenTabelForm.Visible = True
        ElseIf e.CommandName = "EditCommand" Then
            ResetState()
            ID.Text = BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(0).Text
            BidangDokumen.Text = BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(1).Text
            Deskripsi.Text = BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(2).Text
            If BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(3).Text = "Aktif" Then
                Status.SelectedValue = 0
            ElseIf BidangDokumenGridView.Rows(e.CommandArgument.ToString).Cells(3).Text = "Pasif" Then
                Status.SelectedValue = 1
            End If
            BidangDokumenTabelForm.Visible = True
        End If
    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles AddButton.Click
        BidangDokumenTabelForm.Visible = True
        ID.Text = "0"
    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveButton.Click
        CurrentID = ID.Text
        CurrentBidangDokumen = BidangDokumen.Text

        If Equals("0", ID.Text) Then
            If Not bidangDokumenDb.isExist(BidangDokumen.Text) Then
                bidangDokumenDb.insert(BidangDokumen.Text, Deskripsi.Text, Status.SelectedValue)
                BidangDokumenGridView.DataBind()
            End If
        Else
            bidangDokumenDb.update(ID.Text, BidangDokumen.Text, Deskripsi.Text, Status.SelectedValue)
            BidangDokumenGridView.DataBind()

        End If
        ResetState()
    End Sub

    Protected Sub BidangDokumenGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles BidangDokumenGridView.SelectedIndexChanged
        '   BidangDokumen.Text = BidangDokumenGridView.SelectedRow.Cells(1).Text
        '  Deskripsi.Text = BidangDokumenGridView.SelectedRow.Cells(2).Text
        '  If BidangDokumenGridView.SelectedRow.Cells(3).Text = "Aktif" Then
        'Status.SelectedValue = 0
        '   ElseIf BidangDokumenGridView.SelectedRow.Cells(3).Text = "Pasif" Then
        '  Status.SelectedValue = 1
        '   End If
        '  System.Web.HttpContext.Current.Session("status_action") = False
        '  BidangDokumenTabelForm.Visible = True
    End Sub

    Protected Sub BidangDokumenGridView_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles BidangDokumenGridView.PageIndexChanged
        ResetState()
    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CancelButton.Click
        ResetState()
    End Sub

    Protected Sub BidangDokumenGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles BidangDokumenGridView.PageIndexChanging
        'BidangDokumenGridView.PageIndex = e.NewPageIndex
    End Sub

    <WebMethod()>
    Public Shared Function IsExist(ByVal kodeDivisi As String) As String
        Dim bidangDokumenDb As New BidangDokumenDb(WebConfigurationManager.ConnectionStrings("bisaska").ConnectionString)

        If Equals(CurrentID, "0") And bidangDokumenDb.isExist(kodeDivisi) Then
            Return "1"
        ElseIf (Not Equals(CurrentID, "0")) Then
            Return "1"
        End If

        Return "0"
    End Function

    Protected Sub MainScriptManager_AsyncPostBackError(ByVal sender As Object, ByVal e As System.Web.UI.AsyncPostBackErrorEventArgs)
        MainScriptManager.AsyncPostBackErrorMessage =  "An unspecified error occurred."
    End Sub

End Class